package com.mindao.app.weixin.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MediaApi;
import com.jfinal.weixin.sdk.api.MediaApi.MediaType;
import com.mindao.app.common.spring.model.BaseModel;
import com.mindao.app.common.spring.service.BaseService;
import com.mindao.app.weixin.model.WbmpNewsItem;
import com.mindao.app.weixin.model.WxmpMaterial;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年7月17日 下午8:16:42 	
 **/
@Service( "wxmpMaterialService")
public class WxmpMaterialServiceImpl implements WxmpMaterialService {
	private final Logger log = LoggerFactory.getLogger(WxmpMaterialServiceImpl.class);
	@javax.annotation.Resource(name = "baseService")
	private BaseService baseService;
	
	public Integer updateMaterials(MediaType type){
		// TODO Auto-generated method stub
		//得到图片
		ApiResult imgResult= null;
		Date downloadTime=new Date();
		int offset=0;//从全部素材的该偏移位置开始返回，0表示从第一个素材 返回
		int count=20;//返回素材的数量，取值在1到20之间
		int total_count =20; //第一次假设有20条数据
		int item_count =0; //第一次假设只读了0条
		while (offset +item_count<total_count){
			offset=offset+item_count;
		    imgResult= MediaApi.batchGetMaterial(type, offset, count);
		    total_count = imgResult.getInt("total_count");
		    item_count = imgResult.getInt("item_count");
		    log.info("共"+ total_count+"个"+type.get()+",本次下载从第"+offset+"开始下载了"+item_count+"条记录");
		    offset=offset+item_count;
			List<Map> items=  imgResult.getList("item");
			
			if (!MediaType.NEWS.equals(type)){
				for (int i=0;i<items.size();i++) {
					Map map = items.get(i);
					//String media_id, String name, String url,String update_time, Date downLoadTime
					if (!StringUtils.isBlank((String)map.get("url"))){
						//System.out.println("media_id:"+map.get("media_id") +";name:"+map.get("name")+";url:"+map.get("url") );
						WxmpMaterial mat=new WxmpMaterial(type.get(),(String)map.get("media_id"), (String)map.get("name"), (String)map.get("url"),(String)map.get("update_time") ,  downloadTime);
						baseService.create(mat);
					}
				}
				
			}else{
				for (int i=0;i<items.size();i++) {
					Map map = items.get(i);
					System.out.println(map);
					WxmpMaterial mat=new WxmpMaterial(type.get(),(String)map.get("media_id"),((Integer)map.get("update_time")).toString() ,  downloadTime);
					baseService.create(mat);
					Map contentMap =(Map) map.get("content");
					List<Map> news_item=(List)contentMap.get("news_item");
					String name=null;
					for (Map itemMap:news_item){
						//System.out.println("title:"+map.get("title") +"media_id:"+map.get("media_id") +";url:"+map.get("url"));
						String itemTitle=(String)itemMap.get("title");
						name=(name==null)?itemTitle:(name+","+itemTitle);
						//暂时不保存子记录
//						WbmpNewsItem item=new WbmpNewsItem((String)itemMap.get("title"),   (Integer)itemMap.get("show_cover_pic"),   (String)itemMap.get("author"),
//								(String)itemMap.get("digest"),   (String)itemMap.get("content"),    (String)itemMap.get("url"),
//								(String)itemMap.get("content_source_url"),   mat);
//						baseService.create(item);
					}
					mat.setName(name);
					baseService.update(mat);
				}	
			}
			List<WxmpMaterial> delList=baseService.find("from WxmpMaterial where downloadTime!=? and status>-1",downloadTime);
			for (WxmpMaterial o : delList) {
				o.setStatus(BaseModel.STATUS_REMOVED);
				baseService.update(o);
			}
			return total_count;
		}
		 
		return null;
	}

}
